<?php
/**
 * 数据同步文件
 * @author Tony
 * 统一返回json格式：status=1[1:为成功，0:为失败]
 */
$status = 0;//默认为失败
if(!$_POST){
	print_r(urldecode(json_encode(array('status'=>$status,'msg'=>urlencode('缺少参数')))));die;
}
$mobile = $_POST['mobile'];//手机作为唯一标识
if(!$mobile){
	print_r(urldecode(json_encode(array('status'=>$status,'msg'=>urlencode('缺少唯一标识')))));die;
}else{
	$msg = '';
	require_once("inc/config.php");
	require_once("inc/mysql.php");
	global $db;
	$db=new mysql();
	$db->link(DB_SEREVER,DB_DBNAME,DB_USER,DB_PASSWORD,DB_PORT);
	$user_db = 'mx_app_user';//用户表	
	$userInfo = $db->getinfo("select * from $user_db where mobile='$mobile'");
	if(!$userInfo){
		$user = $_POST['user'];//当前用户信息，array
		$user_insert_fields = array();
		if($user){
			//如果没有当前用户，创建
			$user_insert_fields = array(
					'username'	=>	$mobile,
					'password'	=>	md5($mobile),
					'realname'	=>	$user['realname'],
					'groupid'	=>	$user['groupid'],
					'sex'		=>	$user['sex'],
					'mobile'	=>	$mobile,
					'qq'		=>	$user['qq'],
					'mail'		=>	$user['mail'],
					'isok'		=>	$user['isok'],
					'money'		=>	$user['money'],
					'integral'	=>	$user['integral'],
					'regdate'	=>	date('Y-m-d H:i:s')
			);
			if($db->exe_insert($user_db, $user_insert_fields)){
				$userInfo = $db->getinfo("select * from $user_db where username='$mobile'");
			}else{
				$msg = '创建用户失败';
				print_r(urldecode(json_encode(array('status'=>$status,'msg'=>urlencode($msg)))));die;
			}
		}else{
			$msg = '缺少用户信息';
			print_r(urldecode(json_encode(array('status'=>$status,'msg'=>urlencode($msg)))));die;
		}
	}
	if($userInfo){
		$style = intval($_POST['style']);//操作数据类型：1:用户现金；2:用户积分；3:现金和积分
		$type = intval($_POST['type']);//操作数据类型：1:增加；2:减少
		$handle = $_POST['handle'];//操作数组
		$moneyArray = $handle['money'];//现金数组
		$moneyValue = intval($moneyArray['value']);//现金（消费/充值）
		$moneyIntro = $moneyArray['intro'];//现金操作介绍
		$integralArray = $handle['integral'];//积分数组
		$integralValue = intval($integralArray['value']);//积分（兑换/获得）
		$integralIntro = $integralArray['intro'];//积分操作介绍
		if($type && $style){
			$user_update_fields = array();
			if($style == 3){
				//现金and积分
				//现金
				if($moneyValue){
					$type_money_fields = array(
							'userid'	=>	$userInfo['id'],
							'number'	=>	$moneyValue,
							'type'		=>	$type,
							'intro'		=>	$moneyIntro,
							'createtime'=>	time()
					);
					if(!$db->exe_insert('mx_app_user_money', $type_money_fields)){
						$msg = '现金记录入库失败';
					}
				}
				if($integralValue){
					//积分
					$type_integral_fields = array(
							'userid'	=>	$userInfo['id'],
							'integral'	=>	$integralValue,
							'type'		=>	$type,
							'intro'		=>	$integralIntro,
							'createtime'=>	time()
					);
					if(!$db->exe_insert('mx_app_user_integral', $type_integral_fields)){
						$msg = '积分记录入库失败';
					}
				}
				if($type == 1){
					$user_update_fields = array(
							'money'	=>	$userInfo['money']+$moneyValue,
							'integral'	=>	$userInfo['integral']+$integralValue
					);
				}elseif($type == 2){
					$user_update_fields = array(
							'money'	=>	$userInfo['money']-$moneyValue,
							'integral'	=>	$userInfo['integral']-$integralValue
					);
				}
				//更改现金和积分
				if($db->exe_update($user_db, $user_update_fields,"id=".$userInfo['id'])){
					$status = 1;
				}else{
					$msg = '现金和积分更新失败';
				}
			}else{
				if($style == 1 && $moneyValue){
					//现金
					$type_db = 'mx_app_user_money';
					$type_insert_fields = array(
						'userid'	=>	$userInfo['id'],
						'number'	=>	$moneyValue,
						'type'		=>	$type,
						'intro'		=>	$moneyIntro,
						'createtime'=>	time()
					);
					if($type == 1){
						$user_update_fields = array(
							'money'	=>	$userInfo['money']+$moneyValue
						);
					}elseif($type == 2){
						$user_update_fields = array(
							'money'	=>	$userInfo['money']-$moneyValue
						);
					}
				}elseif($style == 2 && $moneyValue){
					//积分
					$type_db = 'mx_app_user_integral';
					$type_insert_fields = array(
							'userid'	=>	$userInfo['id'],
							'integral'	=>	$integralValue,
							'type'		=>	$type,
							'intro'		=>	$integralIntro,
							'createtime'=>	time()
					);
	
					if($type == 1){
						$user_update_fields = array(
								'integral'	=>	$userInfo['integral']+$integralValue
						);
					}elseif($type == 2){
						$user_update_fields = array(
								'integral'	=>	$userInfo['integral']-$integralValue
						);
					}
				}
				//现金和积分记录
				if($db->exe_insert($type_db, $type_insert_fields)){
					//更改现金和积分
					if($db->exe_update($user_db, $user_update_fields,"id=".$userInfo['id'])){
						$status = 1;
					}else{
						$msg = '现金/积分更新失败';
					}
				}else{
					$msg = '现金/积分记录入库失败';
				}
			}
		}
		print_r(urldecode(json_encode(array('status'=>$status,'msg'=>urlencode($msg)))));
	}
}
?>